package xxy.com.yangproject.network;


import android.content.Intent;

import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;

import java.lang.reflect.ParameterizedType;
import java.net.UnknownHostException;
import java.util.concurrent.TimeoutException;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import xxy.com.yangproject.global.Constant;
import xxy.com.yangproject.model.BaseResponseBean;

public abstract class MyObsereveListener<T extends BaseResponseBean> implements Observer<T> {


    @Override
    public void onSubscribe(Disposable d) {

    }

    @Override
    public void onNext(T value) {
        try {
            if (Constant.RESPONSE_CODE_LOGIN_INVALID == value.result) {
//                SPUtils.remove(MHRApplication.application, Constants.SP_KEY_UID);
//                SPUtils.remove(MHRApplication.application, Constants.SP_KEY_TOKEN);
//                SPUtils.remove(MHRApplication.application, Constants.SP_KEY_PHONE);
//                SPUtils.remove(MHRApplication.application, Constants.SP_KEY_BIRTHDAY);
//                SPUtils.remove(MHRApplication.application, Constants.SP_KEY_SEX);
//                SPUtils.remove(MHRApplication.application, Constants.SP_KEY_ALIAS);
//                SPUtils.remove(MHRApplication.application, Constants.SP_KEY_HEAD_THUMB);
//                LocalBroadcastManager.getInstance(MHRApplication.application)
//                        .sendBroadcast(new Intent(Constants.ACTION_LOGOUT_OK));
                value.description = "账号被登出";
            }
            onResponse(value);
        } catch (Exception e){
            ToastUtils.showShort(e.getMessage());
        }
    }

    @Override
    public void onError(Throwable throwable) {
        try {
        ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass();
        for (int i = 0; i < genericSuperclass.getActualTypeArguments().length; i++) {
            LogUtils.eTag(Constant.TAG, genericSuperclass.getActualTypeArguments()[i]);
        }
        final String clazzName = genericSuperclass.getActualTypeArguments()[0].toString();
        //输出不同情况的错误信息, 那容器是什么? 首先需要类名
        T response = (T) Class.forName(clazzName.substring(clazzName.indexOf("com."))).newInstance();

            if (throwable instanceof UnknownHostException) {
                response.description = "网络未连接，请检查网络连接！";
            }else if (throwable instanceof TimeoutException) {
                response.description = "网络连接异常，请检查网络连接！";
            } else if (throwable.getClass().getSimpleName()
                    .startsWith("com.fasterxml.jackson")) {
                response.description = "请求失败！";
            } else {
                if (throwable.getMessage().startsWith("Cannot construct instance")) {
                    response.description = "请求失败！";
                } else {
                    response.description = throwable.getMessage();
                }
            }
            response.result = -1000;
            onResponse(response);
        } catch (final Exception e) {
            LogUtils.eTag(Constant.TAG, e.toString());
        }
    }

    @Override
    public void onComplete() {

    }

    public abstract void onResponse(T t);

//    public abstract void fail();
}
